package com.jy.novel.service;

import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Page;
import com.jy.novel.dao.NovelCategoryDao;
import com.jy.novel.pojo.NovelCategory;
import com.jy.novel.pojo.form.NovelCategoryAddForm;
import com.jy.novel.pojo.form.NovelCategoryEditForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jihb on 4/28/16.
 */
@Service
public class NovelCategoryService {

    @Autowired
    NovelCategoryDao categoryDao;
    /**
     * 查询分类列表 分页
     * @param parentId
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<Page<NovelCategory>> findNovelCategoryPage(long parentId, int curPage, int pageSize){
        return CommonResult.returnDataWrap(categoryDao.findNovelCategoryPage(parentId,curPage,pageSize));
    }

    /**
     * 查询分类列表 分页
     * @param parentId
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<List<NovelCategory>> findNovelCategoryList(long parentId, int curPage, int pageSize){
        return CommonResult.returnDataWrap(categoryDao.findNovelCategoryList(parentId,curPage,pageSize));
    }

    /**
     * 查询分类列表 不分页
     * @param parentId
     * @return
     */
    public CommonResult<List<NovelCategory>> findNovelCategoryList(long parentId){
        return CommonResult.returnDataWrap(categoryDao.findNovelCategoryList(parentId));
    }

    /**
     * 添加分类
     * @param addForm
     * @return
     */
    public CommonResult addNovelCategory(NovelCategoryAddForm addForm){
        if (addForm == null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (StringUtils.isBlank(addForm.getName())){
            return CommonResult.returnFailure("缺少分类名称");
        }
        if (addForm.getParentId()==null || addForm.getParentId() < 0){
            addForm.setParentId(0l);
        }
        if (addForm.getLeaf() == null){
            addForm.setLeaf(false);
        }
        if (addForm.getLevel() == null){
            addForm.setLevel(1);
        }
        return CommonResult.returnBoolWrap(categoryDao.addNovelCategory(addForm),"添加分类失败");
    }

    /**
     * 修改分类
     * @param editForm
     * @param id
     * @return
     */
    public CommonResult updateNovelCategory(NovelCategoryEditForm editForm, long id){
        if (editForm==null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (id < 1){
            return CommonResult.returnFailure("分类id错误");
        }
        return CommonResult.returnBoolWrap(categoryDao.updateNovelCategory(editForm,id),"修改分类失败");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    public CommonResult deleteNovelCategory(long id){
        if (id < 1){
            return CommonResult.returnFailure("分类id错误");
        }
        return CommonResult.returnBoolWrap(categoryDao.deleteNovelCategory(id),"删除分类失败");
    }
}
